﻿
#region using

using System;
using System.Web;
using System.Web.UI;
using System.Net;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;
using BlogEngine.Core;

#endregion

namespace Controls
{
    public class Flickr : Control
    {
        private string _userid;
        private int _images = 4;
        private string _style;
        private int _heightwidth;
        private string _apikey;
        private string _secret;
        private string _visittext;
        ExtensionSettings settings;

        static Flickr() {}

        #region Properties

        ///// <summary>
        ///// User ID of your Flickr account, ex 12345678@N00
        ///// </summary>
        //public string UserID
        //{
        //    get { return _userid; }
        //    set { _userid = value; }
        //}

        ///// <summary>
        ///// Number of images to view
        ///// </summary>
        //public int NumberOfImages
        //{
        //    get { return _images; }
        //    set { _images = value; }
        //}

        ///// <summary>
        ///// CSS style on the image
        ///// </summary>
        //public string Style
        //{
        //    get { return _style; }
        //    set { _style = value; }
        //}

        ///// <summary>
        ///// Height and width of images, square
        ///// </summary>
        //public int HeightWidth
        //{
        //    get { return _heightwidth; }
        //    set { _heightwidth = value; }
        //}
        
        #endregion

        public override void RenderControl(HtmlTextWriter writer)
        {
            bool render = true;
            string tmp;

            try
            {
                this.settings = ExtensionManager.GetSettings("Flickr");

                this._userid = this.settings.GetSingleValue("userid");
                this._style = this.settings.GetSingleValue("style");

                tmp = this.settings.GetSingleValue("images");
                if (tmp != String.Empty)
                    this._images = int.Parse(tmp);
                else
                    this._images = 10;


                tmp = this.settings.GetSingleValue("heightwidth");
                if (tmp != String.Empty)
                    this._heightwidth = int.Parse(tmp);
                else
                    this._heightwidth = 0;

                this._apikey = this.settings.GetSingleValue("apikey");
                this._secret = this.settings.GetSingleValue("secret");
                this._visittext = this.settings.GetSingleValue("visittext");

                if (this._images <= 0)
                    this._images = 4;

                if (this._userid == String.Empty)
                {
                    writer.Write("Must supply Flickr User ID.");
                    render = false;
                }

                if (this._apikey == String.Empty)
                {
                    writer.Write("Must supply Flickr API-key.");
                    render = false;
                }

                if (this._secret == String.Empty)
                {
                    writer.Write("Must supply Flickr secret key.");
                    render = false;
                }

                if (render == false)
                    return;
                else
                    writer.Write(GetPhotoStream());
            }
            catch (Exception ex)
            {
                writer.Write(ex.Message);
            }
        }

        private string GetPhotoStream()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<p id=\"flickr\">");

                // Get photostream
                FlickrNet.Flickr.CacheDisabled = true;
                FlickrNet.Flickr flickr = new FlickrNet.Flickr(this._apikey, this._secret);
                FlickrNet.PhotoSearchOptions options = new FlickrNet.PhotoSearchOptions();
                options.UserId = this._userid;
                options.PerPage = this._images;
                options.Page = 1;
                options.SortOrder = FlickrNet.PhotoSearchSortOrder.DatePostedDesc;

                FlickrNet.Photos photos = flickr.PhotosSearch(options);

                if (photos.PhotoCollection == null)
                    sb.Append("No photos available.");
                else
                {
                    // Display photos
                    foreach (FlickrNet.Photo photo in photos.PhotoCollection)
                    {
                        sb.AppendFormat("<a href=\"{0}\"><img src=\"{1}\" title=\"{2}\" alt=\"{2}\" style=\"{3}\"{4} /></a>",
                            photo.WebUrl, photo.SquareThumbnailUrl, photo.Title, this._style,
                            (_heightwidth != null && _heightwidth > 0) ? " width=\"" + this._heightwidth + "\" height=\"" + this._heightwidth + "\"" : "");
                    }
                }

                if (this._visittext != String.Empty)
                    sb.AppendFormat("<a href=\"{0}\" title=\"{1}\">{1}</a>", flickr.UrlsGetUserPhotos(this._userid).AbsoluteUri, this._visittext);
                sb.Append("</p>");

                // Return string
                return sb.ToString();
            }
            catch( Exception ex )
            {
                return ex.Message;
            }
        }
    }
}